//
//  BingFaZhengController.m
//  QuickCure37
//
//  Created by 马文星 on 16/6/24.
//  Copyright © 2016年 Demos. All rights reserved.
//

#import "BingFaZhengController.h"
#import "MLSelectBingFaCell.h"
#import "BingFaZhengDTO.h"

@interface BingFaZhengController ()<UITableViewDelegate,UITableViewDataSource>


@property (nonatomic, strong) UITableView *tableview;

@property (nonatomic, strong) NSArray *dtoArray;


@property (nonatomic, strong) NSMutableArray *result_Array;

@end

@implementation BingFaZhengController

static NSString *ID = @"selectCell";

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupUI];
    [self addRightButtonItemWithTitle:@"确定" titleColor:BLACK_COLOR];
}

- (void)setupUI {
    
    self.title = @"并发症";
    
    [self.view addSubview:self.tableview];
    
    [self.tableview mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.top.right.bottom.mas_equalTo(0);
    }];
}

#pragma mark  - UITableViewDataSource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.dtoArray.count;
    
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    MLSelectBingFaCell *cell = (MLSelectBingFaCell *)[tableView dequeueReusableCellWithIdentifier:ID];
    
    if (!cell) {
        
        cell = [[MLSelectBingFaCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
        
        //第三步：回调刷新
        cell.clickBingFaZhengCellBlock = ^(NSIndexPath *indexpath){

            [tableView reloadData];
        };

    }
    
    cell.indexpath = indexPath;
    
    cell.smlButton.selected = NO;

    
    BingFaZhengDTO *dto = self.dtoArray[indexPath.row];
    
     cell.bingfaDto = dto;
    
    
    /// 第四步：筛选结果
    /// 点击 是要加上去的
    if (dto.isSelected) {
        
        cell.smlButton.selected = YES;

        [cell.smlButton setTitle:@"已订阅" forState:UIControlStateNormal];

        
        if (![self.result_Array containsObject:dto]) {
            
            [self.result_Array addObject:dto];
        }
        
    /// 取消 是要 减去 的
    }else{
    
        cell.smlButton.selected = NO;
        
        [cell.smlButton setTitle:@"订阅" forState:UIControlStateNormal];
        
        if ([self.result_Array containsObject:dto]) {
            
            [self.result_Array removeObject:dto];
        }
    
    }

    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    

    loggerInfo(@"正在开发中");
        
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

#pragma mark  - ACtions
- (void)clickRightItem:(UIButton *)btn{

    NSMutableString *caseString = [NSMutableString string];
    
    for (int i = 0 ;i < self.result_Array.count ;i++) {
        
        BingFaZhengDTO *dto = self.result_Array[i];

        [caseString appendString:dto.name];
        
        if ( i==self.result_Array.count-1) {
        
            continue;
        }
        [caseString appendString:@"、"];
    }
    
    if (self.clickBingFaZhengVCBlock) {
        
        self.clickBingFaZhengVCBlock(caseString);
    }
    
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark  - Lazy
-(UITableView *)tableview{
    
    if (_tableview == nil) {
        _tableview = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableview.dataSource = self;
        _tableview.delegate = self;
        _tableview.tableFooterView = [UIView new];
        
        UILabel *headLab = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 60)];
        headLab.text = @" 注意：1.下面的cell一个都不能少,测试复用  2.点击cell不做事情，点击btn改变文字和字体，再次点击可以取消";
        headLab.font = FONT(15);
        headLab.textColor = RED_COLOR;
        headLab.numberOfLines = 0;
        _tableview.tableHeaderView = headLab;
    }
    return _tableview;
}

-(NSArray *)dtoArray{
    
    if (_dtoArray == nil) {
        _dtoArray = [[NSArray alloc] init];
        
        NSArray *array = @[@"缺铁性贫血1型",@"胃穿孔1型",@"呕血1型",@"黑粪1型",@"胃肠出血1型",@"缺铁性贫血2型",@"胃穿孔2型",@"呕血2型",@"黑粪2型",@"胃肠出血2型",@"缺铁性贫血3型",@"胃穿孔3型",@"呕血3型",@"黑粪3型",@"胃肠出血3型"];
        
        NSMutableArray *arrM = [NSMutableArray arrayWithCapacity:10];
        for (int i = 0; i < array.count; ++i) {
            
            BingFaZhengDTO *dto = [[BingFaZhengDTO alloc] init];
            dto.name = array[i];
            dto.isSelected = NO;
            
            [arrM addObject:dto];
        }
    
        _dtoArray = arrM.copy;
    }
    return _dtoArray;
}

/// 索引数组
-(NSMutableArray *)result_Array{
    
    if (_result_Array == nil) {
        _result_Array = [NSMutableArray arrayWithCapacity:10];
    }
    return _result_Array;
}


@end
